package com.zcxy.sys.controller;


import cn.hutool.core.util.IdUtil;
import com.fasterxml.jackson.annotation.JsonInclude;

import com.zcxy.core.bean.Response;
import com.zcxy.sys.constant.Constant;
import com.zcxy.sys.pojo.LoginVo;
import com.zcxy.sys.pojo.SysUserLoginDto;
import com.zcxy.sys.pojo.SysUserResetPassWord;
import com.zcxy.sys.service.IEmailService;
import com.zcxy.sys.service.ISysUserService;
import com.zcxy.sys.service.IVerificationCodeService;
import com.zcxy.util.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author zcc
 * @version 1.0.0
 * @ClassName SysUserController.java
 * @Description 用户
 * @createTime 2020-06-01
 */
@RestController
@RequestMapping("/sys")
@Api(tags = "<font  color='red'>登录相关接口</font>")
@Slf4j
@JsonInclude(JsonInclude.Include.NON_NULL)
public class LoginController {

    @Autowired
    private ISysUserService iSysUserService;


    @Autowired
    private IVerificationCodeService verificationCodeService;

    @Autowired
    private IEmailService emailService;


    @PostMapping(value = "/login")
    @ApiOperation(value = "登录", notes = "登录")
    public Response<LoginVo> login(@RequestBody SysUserLoginDto sysUser) {
        return Response.ok(iSysUserService.login(sysUser));
    }




    @ApiOperation("登出")
    @PostMapping(value = "/loginout")
    public Response LoginOut() {
        iSysUserService.LoginOut();
        return Response.ok();
    }


    @PostMapping(value = "/email/resetPass")
    @ApiOperation("重置密码，发送验证码")
    public Response resetPass(@RequestParam String email) throws Exception {
        //校验邮箱是否存在
        iSysUserService.checkEmail(email);

        com.zcxy.sys.entity.VerificationCode code = new com.zcxy.sys.entity.VerificationCode();
        code.setType("email");
        code.setValue(email);
        code.setScenes(Constant.RESET_MAIL);
        com.zcxy.sys.pojo.EmailVo emailVo = iSysUserService.sendEmail(code);
        emailService.send(emailVo);
        return Response.ok();
    }

    @PostMapping(value = "/validated")
    @ApiOperation("验证验证码")
    public Response validated(@RequestBody com.zcxy.sys.entity.VerificationCode code) {
        iSysUserService.validated(code);
        return Response.ok();
    }

    @PostMapping(value = "/updatePwd")
    @ApiOperation("更新用户密码")
    public Response updatePwd(@RequestBody @Validated SysUserResetPassWord code) {
        iSysUserService.updatePwd(code);
        return Response.ok();
    }

}
